// pages/MoreImage/MoreImage.js
const app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true,
    videolist: [],
    isOver: false,
    isCollect: false, // 默认没有收藏
    urls: [],
    loaymore: [],
    pageindex: 1,
    callbackcount: 15,
    loadSongList: [], //放置返回数据的数组
    isFromSearch: true, // 用于判断loadSongList数组是不是空数组，默认true，空的数组
    loadLoading: false, //"上拉加载"的变量，默认false，隐藏
    loadLoadingComplete: false, //“没有数据”的变量，默认false，隐藏
    loadText: "加载更多",
    page: 1,
    rows: 8
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo(options);
  },
  initData: function (options) {
    this.setData({
      marineId: options.marineId,
      activityId: options.activityId
    })
    this.queryActResourceList();
    this.queryActInfo(options);
  },
  queryActInfo: function(options) {

    let that = this
    // 视频
    wx.request({
      url: app.httpUrl + '/activity/queryActInfo/' + options.activityId,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        var activity = res.data.data;
        that.setData({ activity: activity})
      }
    })
  },
  lower: function(e) {
    if (!this.data.isOver) {
      this.queryActResourceList();
    }
  },
  queryActResourceList: function() {
    var that = this;
    wx.request({
      url: app.httpUrl + '/activity/live/queryListByPage',
      data: {
        activityId: that.data.activityId,
        marineId: that.data.marineId,
        page: this.data.page,
        rows: this.data.rows,
        liveType: "video"
      },
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function(res) {
        if (that.data.page >= res.data.data.totalPage) {
          that.setData({
            isOver: true,
            successSearchLoadingComplete: true
          })
        }
        that.data.page += 1;
        var videolist = that.data.videolist;
        var list = res.data.data.results;
        for (var i in list) {
          list[i].createDate = util.formatDate(list[i].createTime, 'Y-M-D')
          for (var j in list[i].attachList) {
            if (j == 0) {
              list[i].firstVideoUrl = app.imgUrl + list[i].attachList[j].attachUrl;
              list[i].firstThumbUrl = app.imgUrl + list[i].attachList[j].thumbAttachUrl;
            }
          }
          videolist.push(list[i]);
        }
        that.setData({
          videolist: videolist
        })
      }
    })
  },
  addActivityZan: function(event) {
    var liveid = event.currentTarget.dataset.liveid;
    var idx = event.currentTarget.dataset.idx;
    var that = this;
    wx.request({
      url: app.httpUrl + '/activity/live/addActivityZan',
      data: {
        liveId: liveid
      },
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function(res) {
        if (res.data.code == '000') {
          var list = that.data.videolist;
          list[idx].zanId = res.data.data.id;
          list[idx].zanCount = list[idx].zanCount + 1;
          that.setData({
            videolist: list
          })
        }
      }
    })
  },
  addClicks: function (id) {
    wx.request({
      url: app.httpUrl + '/activity/live/addClicks',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        id: id
      },
      success: (res) => {
      }
    })
  },
  // 加载更多
  loadMore: function(e) {
    let index = e.currentTarget.dataset.index;
    this.queryActResourceList(index);
  },
  playVideo(e) {
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    let list = this.data.videolist;
    list.forEach(n => {
      n.isPlaying = false;
    })
    list[index].isPlaying = true;
    list[index].clicks = list[index].clicks + 1;
    this.setData({
      videolist: list
    })
    let videoContext = wx.createVideoContext('video-' + index)
    videoContext.play()
    this.addClicks(id);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '@快乐 分享我的精彩瞬间 From陶学趣',
      path: '/pages/MoreVideo/MoreVideo?activityId=' + this.data.activityId + '&marineId=' + this.data.marineId,
      imageUrl: app.imgUrl + this.data.activity.activityImgUrl, //这是我的图片路径
      success: function (res) {
        //转发成功
      }
    }
  },
  getUserInfo: function (options) {
    let that = this;
    if (app.globalData.appUser != null) {
      this.initData(options);
      return;
    }
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.httpUrl + '/common/getUserInfo',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: "POST",
            success: function (res) {
              if (res.data.code == '000') {
                app.globalData.appUser = res.data.data;
                app.globalData.openid = res.data.data.openid;
                that.initData(options);
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  }
})